package com.tuvi.nhamthin.activity;


import com.tuvi.nhamthin.api.ITuvi;
import com.tuvi.nhamthin.api.TuviApi;
import com.tuvi.nhamthin.common.DeviceFactory;
import com.tuvi.nhamthin.helper.SqlHelper;
import com.tuvi.nhamthin.services.BackgroundMusic;
import com.tuvi.nhamthin.ui.UIMainScreen;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.widget.Gallery;


public class Tuvi extends BaseActivity {
		
    @Override
    public void onCreate(Bundle paramBundle){
      super.onCreate(paramBundle);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      Object localObject = getWindowManager().getDefaultDisplay();
      localObject = new UIMainScreen(this, ((Display)localObject).getWidth(), ((Display)localObject).getHeight());
      ((UIMainScreen)localObject).setLayoutParams(new Gallery.LayoutParams(-1, -1));
      ((UIMainScreen)localObject).setOnTouchListener((View.OnTouchListener)localObject);
      setContentView((View)localObject);
      RegisterTask register = new RegisterTask();
      register.execute();
    }
    @Override
    public void onStop(){
    	super.onStop();
    }
    @Override
    public void onResume(){
    	super.onResume();
    }
    private void register(){
    	SqlHelper sqlHelper = new SqlHelper(this);
    	sqlHelper.createDatabase();
    	sqlHelper.openDataBase();
    	if (!sqlHelper.isUserAdded()) {
    		Log.d("Add user to server", "Add user to server");
    		if(registerToServer()){
    			//Update to database
    			sqlHelper.addUser();
    		}
		}
    	sqlHelper.close();
    }
    private boolean registerToServer(){
    	ITuvi api = new TuviApi();
    	return api.insertNewUserUsed(TuviApi.USERNAME,
    			TuviApi.PASSWORD, 
    			TuviApi.APP_ID, 
    			DeviceFactory.getDeviceId(this), 
    			"1", 
    			"ANDROID", 
    			"ANDROID", 
    			"1", 
    			"1", 
    			DeviceFactory.getLanguage());
    }
    private class RegisterTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... paramArrayOfParams) {
			// TODO Auto-generated method stub
			Tuvi.this.register();
			return null;
		}
    	
    }
}